package extrace.ui.domain;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import extrace.loader.HistoryLoader;
import extrace.misc.model.PackageRoute;
import extrace.misc.model.TransHistory;
import extrace.ui.main.ui.RouteFragment;

import extrace.ui.main.ui.TrackService;
import zxing.util.CaptureActivity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.icu.text.Transliterator;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.district.DistrictResult;
import com.amap.api.services.district.DistrictSearch;
import com.amap.api.services.district.DistrictSearchQuery;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;

import extrace.loader.ExpressLoader;
import extrace.misc.model.CustomerInfo;
import extrace.misc.model.ExpressSheet;
import extrace.net.IDataAdapter;
import extrace.ui.main.R;
import extrace.ui.misc.CustomerListActivity;

public class ExpressEditActivity extends AppCompatActivity implements ActionBar.TabListener, IDataAdapter<ExpressSheet> {

//	public static final int INTENT_NEW = 1; 
//	public static final int INTENT_EDIT = 2; 

    public static final int REQUEST_CAPTURE = 100;
    public static final int REQUEST_RCV = 101;
    public static final int REQUEST_SND = 102;

    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    private ExpressSheet mItem;

    private ExpressLoader mLoader;
    private Intent mIntent;
    private ExpressEditFragment1 baseFragment;
    private RouteFragment fragment = new RouteFragment();
    private MenuItem action_menu_item;
    private TrackService trackService;

    private boolean new_es = false;    //新建

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_express_edit);

        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }

        mIntent = getIntent();
        if (mIntent.hasExtra("Action")) {
            if (mIntent.getStringExtra("Action").equals("New")) {
//                new_es = true;
                StartCapture();
            } else if (mIntent.getStringExtra("Action").equals("Query")) {
                StartCapture();
            } else if (mIntent.getStringExtra("Action").equals("Dabao")) {
                StartCapture();
            } else if (mIntent.getStringExtra("Action").equals("Chaibao")) {
                StartCapture();
            } else if (mIntent.getStringExtra("Action").equals("Edit")) {
                ExpressSheet es;
                if (mIntent.hasExtra("ExpressSheet")) {
                    es = (ExpressSheet) mIntent.getSerializableExtra("ExpressSheet");
                    Refresh(es.getID());
                } else {
                    this.setResult(RESULT_CANCELED, mIntent);
                    this.finish();
                }
            } else {
                this.setResult(RESULT_CANCELED, mIntent);
                this.finish();
            }
        } else {
            this.setResult(RESULT_CANCELED, mIntent);
            this.finish();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.express_edit, menu);
        action_menu_item = menu.findItem(R.id.action_action);
        action_menu_item.setVisible(false);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_action:
                if (item.getTitle().equals("收件")) {
                    Receive(mItem.getID());
                    finish();
                } else if (item.getTitle().equals("交付")) {
                    Delivery(mItem.getID());
                    finish();
                } else if (item.getTitle().equals("追踪")) {
                    Tracert(mItem.getID());
                } else if (item.getTitle().equals("打包")) {
                    Dabao(mItem.getID());
                    finish();
                } else if (item.getTitle().equals("拆包")) {
                    Chaibao(mItem.getID());
                    finish();
                } else if (item.getTitle().equals("接收开始派送")) {
                    dispatch(mItem.getID());
                    finish();
                }
                return true;
            case R.id.action_ok:
                Save();
                return true;
            case R.id.action_refresh:
                if (mItem != null) {
                    Refresh(mItem.getID());
                }
                return true;
            case (android.R.id.home):
                mIntent.putExtra("ExpressSheet", mItem);
                this.setResult(RESULT_OK, mIntent);
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
                              FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) {
    }

    @Override
    public ExpressSheet getData() {
        return mItem;
    }

    @Override
    public void setData(ExpressSheet data) {
        mItem = data;
    }

    @Override
    public void notifyDataSetChanged() {
        if (baseFragment != null) {
            baseFragment.RefreshUI(mItem);
        }

        getRoute();
//Toast.makeText(getApplicationContext(),"getRoute()",Toast.LENGTH_SHORT).show();

        MenuDisplay(mItem.getStatus());
        if (mItem.getStatus() == 2 || action_menu_item.getTitle().toString().equals("交付") || action_menu_item.getTitle().toString().equals("已交付")) {

            fragment.setItemID(mItem.getID());

        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {


        CustomerInfo customer;

        switch (resultCode) {
            case RESULT_OK:
                switch (requestCode) {
                    case REQUEST_CAPTURE:
                        if (data.hasExtra("BarCode")) {
                            String id = data.getStringExtra("BarCode");
                            try {
                                mLoader = new ExpressLoader(this, this);
                                if (new_es) {
                                    new_es = false;
                                    mLoader.New(id);
                                } else {
                                    mLoader.Load(id);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        break;
                    case REQUEST_RCV:
                        if (data.hasExtra("CustomerInfo")) {
                            customer = (CustomerInfo) data.getSerializableExtra("CustomerInfo");
                            mItem.setRecever(customer);
                            baseFragment.displayRcv(mItem);
                        }
                        break;
                    case REQUEST_SND:
                        if (data.hasExtra("CustomerInfo")) {
                            customer = (CustomerInfo) data.getSerializableExtra("CustomerInfo");
                            mItem.setSender(customer);
                            baseFragment.displaySnd(mItem);
                        }
                        break;
                }
                break;
            default:
                break;
        }
    }

    void MenuDisplay(int status) {
        action_menu_item.setVisible(true);
        action_menu_item.setEnabled(true);
        switch (status) {
            case ExpressSheet.STATUS.STATUS_CREATED:
                action_menu_item.setTitle("收件");
                break;
            case ExpressSheet.STATUS.STATUS_TRANSPORT:
                if (mIntent.getStringExtra("Action").equals("Dabao"))
                    action_menu_item.setTitle("打包");
                else if (mIntent.getStringExtra("Action").equals("Chaibao"))
                    action_menu_item.setTitle("拆包");
                else if (mIntent.getStringExtra("mExType") != null && mIntent.getStringExtra("mExType").equals("ExDLV"))
                    action_menu_item.setTitle("交付");
                else if (mIntent.getStringExtra("mExType") != null && mIntent.getStringExtra("mExType").equals("ExRCV"))
                    action_menu_item.setTitle("待打包");
                else if (mIntent.getStringExtra("mExType") != null && mIntent.getStringExtra("mExType").equals("ExTAN"))
                    action_menu_item.setTitle("接收开始派送");

                break;
            case ExpressSheet.STATUS.STATUS_DELIVERIED:
                action_menu_item.setTitle("已交付");
                break;
            default:
                action_menu_item.setVisible(false);
                break;
        }
    }

    void Refresh(String id) {
        try {
            mLoader = new ExpressLoader(this, this);
            mLoader.Load(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void Receive(final String id) {
        try {
            mLoader = new ExpressLoader(this, this);
            sendpos(id);
            mLoader.Receive(id);
            Toast.makeText(getApplicationContext(),"快件"+mItem.getID()+"揽收成功",Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void Delivery(String id) {
        try {
            mLoader = new ExpressLoader(this, this);
            mLoader.Delivery(id);

            sendpos(id);
            Intent stopIntent = new Intent(this, TrackService.class);
            stopService(stopIntent);
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void Chaibao(String id) {
        try {
            mLoader = new ExpressLoader(this, this);
            mLoader.Chaibao(id);
            Toast.makeText(getApplicationContext(), "拆包成功，请后续打包或派送", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    void Dabao(final String id) {
        try {
            mLoader = new ExpressLoader(this, this);
            mLoader.Dabao(id);
            sendpos(id);
            Toast.makeText(getApplicationContext(), "打包成功，请后续转运", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    IDataAdapter<List<PackageRoute>> iDataAdapter = new IDataAdapter<List<PackageRoute>>() {
        private List<PackageRoute> packageRoutes;

        @Override
        public List<PackageRoute> getData() {
            return null;
        }

        @Override
        public void setData(List<PackageRoute> data) {
            packageRoutes = data;
        }

        @Override
        public void notifyDataSetChanged() {
            drawRoutes(packageRoutes);
        }
    };

    private void drawRoutes(final List<PackageRoute> packageRoutes) {
        final List<String> address = new ArrayList<>();

        final GeocodeSearch geocoderSearch = new GeocodeSearch(getApplicationContext());
        final int[] i = {0};
            LatLonPoint latLonPoint = new LatLonPoint(packageRoutes.get(i[0]).getX(), packageRoutes.get(i[0]).getY());

            RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 90, GeocodeSearch.AMAP);
            geocoderSearch.getFromLocationAsyn(query);


        geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int ii) {
                i[0]++;
              address.add(regeocodeResult.getRegeocodeAddress().getFormatAddress()) ;
                if(address.size()==packageRoutes.size()){
                    fragment.drawRoutes(packageRoutes, mItem.getStatus(), address);
                }
                if(address.size()<packageRoutes.size()){
                    LatLonPoint latLonPoint = new LatLonPoint(packageRoutes.get(i[0]).getX(), packageRoutes.get(i[0]).getY());
                    RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 90, GeocodeSearch.AMAP);
                    geocoderSearch.getFromLocationAsyn(query);
                }

            }

            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

            }
        });


    }

    //发送当前终端位置
    private void sendpos(final String id) {

        final HistoryLoader historyLoader = new HistoryLoader(iDataAdapter, this);
        final AMapLocationClient locationClient = new AMapLocationClient(getApplicationContext());
        AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                float x = (float) amapLocation.getLongitude();
                float y = (float) amapLocation.getLatitude();
                historyLoader.sendposition(id, y, x);
                locationClient.stopLocation();
//                Toast.makeText(getApplicationContext(), (float) amapLocation.getLongitude() + " ", Toast.LENGTH_SHORT).show();
            }
        };

        locationClient.setLocationListener(mAMapLocationListener);
        locationClient.startLocation();
    }


    void dispatch(String id) {
        try {
            mLoader = new ExpressLoader(this, this);
            mLoader.dispatch(id);

            Intent intent = new Intent(this, TrackService.class);
            intent.putExtra("id", id);
            startService(intent);

            Toast.makeText(getApplicationContext(), "接受成功，请后续派送", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void Tracert(String id) {
        //快件追踪
    }

    void Save() {
        mLoader = new ExpressLoader(this, this);
        mLoader.Save(mItem);


    }

    private void StartCapture() {
        Intent intent = new Intent();
        intent.putExtra("Action", "Captrue");
        intent.setClass(this, CaptureActivity.class);
        startActivityForResult(intent, REQUEST_CAPTURE);
    }

    private void GetCustomer(int intent_code) {
        Intent intent = new Intent();
        intent.setClass(this, CustomerListActivity.class);
        if (intent_code == REQUEST_RCV) {
            if (baseFragment.mRcvNameView.getTag() == null) {
                intent.putExtra("Action", "New");
            } else {
                intent.putExtra("Action", "New");
                intent.putExtra("CustomerInfo", (CustomerInfo) baseFragment.mRcvNameView.getTag());
            }
        } else if (intent_code == REQUEST_SND) {
            if (baseFragment.mSndNameView.getTag() == null) {
                intent.putExtra("Action", "New");
            } else {
                intent.putExtra("Action", "New");
                intent.putExtra("CustomerInfo", (CustomerInfo) baseFragment.mSndNameView.getTag());
            }
        }
        startActivityForResult(intent, intent_code);
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    baseFragment = ExpressEditFragment1.newInstance();
                    return baseFragment;
                case 1:


                    return fragment;
            }
            return ExpressEditFragment1.newInstance();
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_ex_edit1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_ex_edit2).toUpperCase(l);
            }
            return null;
        }
    }

    private List<LatLonPoint> getRoute() {

        if (mItem.getStatus() >= 1) {
            HistoryLoader historyLoader2 = new HistoryLoader(iDataAdapter, this);
            historyLoader2.getPackageroute(mItem.getID());
        }

        final ArrayList<LatLonPoint> latLonPoints = new ArrayList<>();
        final PackageRoute packageRoute = new PackageRoute();
//      packageRoute.setSN(1);
//      packageRoute.setPkg("233");
//      packageRoute.setTm(new Date(System.currentTimeMillis()));
//      packageRoute.setX((float) 39.942295);
//      packageRoute.setY((float)116.335891);

        PackageRoute packageRoute2 = new PackageRoute();
//        packageRoute2.setSN(1);
//        packageRoute2.setPkg("233");
//        packageRoute2.setTm(new Date(System.currentTimeMillis()));
//        packageRoute2.setX((float) 39.995576);
//        packageRoute2.setY((float) 116.481288);

//                        bundle.putSerializable("routes", latLonPoints);


        if (mItem.getSender() != null) {
            GeocodeSearch geocoderSearch = new GeocodeSearch(this);
            GeocodeQuery query = new GeocodeQuery(mItem.getSender().getRegionString(), null);
            GeocodeQuery query2 = new GeocodeQuery(mItem.getRecever().getRegionString(), null);
            geocoderSearch.getFromLocationNameAsyn(query);
            geocoderSearch.getFromLocationNameAsyn(query2);
            final int[] num = {0};

            geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
                @Override
                public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

                }

                @Override
                public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
                    latLonPoints.add(geocodeResult.getGeocodeAddressList().get(0).getLatLonPoint());
                    num[0]++;
                    if (num[0] == 2) {

                        fragment.setroutes(latLonPoints);

                    }
                }
            });

        }
        return latLonPoints;
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class ExpressEditFragment1 extends Fragment {

        private TextView mIDView;
        private TextView mRcvNameView;
        private TextView mRcvTelCodeView;
        private TextView mRcvDptView;
        private TextView mRcvAddrView;
        private TextView mRcvRegionView;

        private TextView mSndNameView;
        private TextView mSndTelCodeView;
        private TextView mSndDptView;
        private TextView mSndAddrView;
        private TextView mSndRegionView;

        private TextView mRcverView;
        private TextView mRcvTimeView;

        private TextView mSnderView;
        private TextView mSndTimeView;

        private TextView mStatusView;

        private ImageView mbtnCapture;
        private ImageView mbtnRcv;
        private ImageView mbtnSnd;

        public static ExpressEditFragment1 newInstance() {
            ExpressEditFragment1 fragment = new ExpressEditFragment1();
            return fragment;
        }

        public ExpressEditFragment1() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_express_edit1,
                    container, false);
            mIDView = (TextView) rootView.findViewById(R.id.expressId);
            mRcvNameView = (TextView) rootView.findViewById(R.id.expressRcvName);
            mRcvTelCodeView = (TextView) rootView.findViewById(R.id.expressRcvTel);
            mRcvAddrView = (TextView) rootView.findViewById(R.id.expressRcvAddr);
            mRcvDptView = (TextView) rootView.findViewById(R.id.expressRcvDpt);
            mRcvRegionView = (TextView) rootView.findViewById(R.id.expressRcvRegion);

            mSndNameView = (TextView) rootView.findViewById(R.id.expressSndName);
            mSndTelCodeView = (TextView) rootView.findViewById(R.id.expressSndTel);
            mSndAddrView = (TextView) rootView.findViewById(R.id.expressSndAddr);
            mSndDptView = (TextView) rootView.findViewById(R.id.expressSndDpt);
            mSndRegionView = (TextView) rootView.findViewById(R.id.expressSndRegion);

            mRcvTimeView = (TextView) rootView.findViewById(R.id.expressAccTime);
            mSndTimeView = (TextView) rootView.findViewById(R.id.expressDlvTime);

            mStatusView = (TextView) rootView.findViewById(R.id.expressStatus);

            mbtnCapture = (ImageView) rootView.findViewById(R.id.action_ex_capture_icon);
            mbtnCapture.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ((ExpressEditActivity) getActivity()).StartCapture();
                        }
                    });
            mbtnRcv = (ImageView) rootView.findViewById(R.id.action_ex_rcv_icon);
            mbtnRcv.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ((ExpressEditActivity) getActivity()).GetCustomer(REQUEST_RCV);
                        }
                    });
            mbtnSnd = (ImageView) rootView.findViewById(R.id.action_ex_snd_icon);
            mbtnSnd.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ((ExpressEditActivity) getActivity()).GetCustomer(REQUEST_SND);
                        }
                    });
            return rootView;
        }

        void RefreshUI(ExpressSheet es) {
            mIDView.setText(es.getID());
            displayRcv(es);
            displaySnd(es);
            if (es.getAccepteTime() != null)
                mRcvTimeView.setText(DateFormat.format("yyyy-MM-dd hh:mm:ss", es.getAccepteTime()));
            else
                mRcvTimeView.setText(null);
            if (es.getDeliveTime() != null)
                mSndTimeView.setText(DateFormat.format("yyyy-MM-dd hh:mm:ss", es.getDeliveTime()));
            else
                mSndTimeView.setText(null);

            String stText = "";
            switch (es.getStatus()) {
                case ExpressSheet.STATUS.STATUS_CREATED:
                    stText = "正在创建";
                    break;
                case ExpressSheet.STATUS.STATUS_TRANSPORT:
                    stText = "运送途中";
                    break;
                case ExpressSheet.STATUS.STATUS_DELIVERIED:
                    stText = "已交付";
                    break;
            }
            mStatusView.setText(stText);
            displayBtn(es);
        }

        void displayBtn(ExpressSheet es) {    //按钮状态控制
            if (es.getStatus() == ExpressSheet.STATUS.STATUS_CREATED) {
                mbtnRcv.setVisibility(View.VISIBLE);
                mbtnSnd.setVisibility(View.VISIBLE);
            } else {
                mbtnRcv.setVisibility(View.INVISIBLE);
                mbtnSnd.setVisibility(View.INVISIBLE);
            }
        }

        void displayRcv(ExpressSheet es) {
            if (es.getRecever() != null) {
                mRcvNameView.setText(es.getRecever().getName());
                mRcvTelCodeView.setText(es.getRecever().getTelCode());
                mRcvNameView.setTag(es.getRecever());
                mRcvAddrView.setText(es.getRecever().getAddress());
                mRcvDptView.setText(es.getRecever().getDepartment());
                mRcvRegionView.setText(es.getRecever().getRegionString());
            } else {
                mRcvNameView.setText(null);
                mRcvTelCodeView.setText(null);
                mRcvNameView.setTag(null);
                mRcvAddrView.setText(null);
                mRcvDptView.setText(null);
                mRcvRegionView.setText(null);
            }
        }

        void displaySnd(ExpressSheet es) {
            if (es.getSender() != null) {
                mSndNameView.setText(es.getSender().getName());
                mSndTelCodeView.setText(es.getSender().getTelCode());
                mSndNameView.setTag(es.getSender());
                mSndAddrView.setText(es.getSender().getAddress());
                mSndDptView.setText(es.getSender().getDepartment());
                mSndRegionView.setText(es.getSender().getRegionString());
            } else {
                mSndNameView.setText(null);
                mSndTelCodeView.setText(null);
                mSndNameView.setTag(null);
                mSndAddrView.setText(null);
                mSndDptView.setText(null);
                mSndRegionView.setText(null);
            }
        }
    }

    public static class ExpressEditFragment2 extends Fragment {

        /**
         * Returns a new instance of this fragment for the given section number.
         */
        public static ExpressEditFragment2 newInstance() {
            ExpressEditFragment2 fragment = new ExpressEditFragment2();
//			Bundle args = new Bundle();
//			args.putInt(ARG_SECTION_NUMBER, sectionNumber);
//			fragment.setArguments(args);
            return fragment;
        }

        public ExpressEditFragment2() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_express_edit2,
                    container, false);
//			TextView textView = (TextView) rootView
//					.findViewById(R.id.section_label);
//			textView.setText(Integer.toString(getArguments().getInt(
//					ARG_SECTION_NUMBER)));
            return rootView;
        }
    }


}
